//
//  DebugManager.m
//  SuDian
//
//  Created by coder on 2019/5/17.
//  Copyright © 2019 DHK. All rights reserved.
//

#import "DebugManager.h"
#import <UIKit/UIKit.h>

@implementation DebugModel



@end

#define ENVIRONMENTKEY @"environmentKey"

static DebugManager *debugManager = nil;

@interface DebugManager ()

@property (nonatomic, copy) void(^initializeBlock)(void);
@property (nonatomic, copy) void(^changeEnvBlock)(void);
@property (nonatomic, strong) NSArray <DebugModel *>* environments;
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, strong) DebugModel *currentModel;

@end

@implementation DebugManager

+ (id)shareManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        debugManager = [[self alloc] init];
        debugManager.lastIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:ENVIRONMENTKEY] integerValue];
    });
    return debugManager;
}

+ (id)alloc {
    @synchronized (self) {
        if (!debugManager) {
            return [super alloc];
        }
    }
    return debugManager;
}

- (void)setupRelease:(DebugModel *)releaseModel debug:(NSArray <DebugModel *>*)debugModels {
#ifdef RELEASE
    self.currentModel = releaseModel;
#else
    self.environments = debugModels;
    if (self.lastIndex < self.environments.count) {
        self.currentModel = self.environments[self.lastIndex];
    }else {
        self.currentModel = self.environments.firstObject;
    }
#endif
}

- (void)initialize:(void(^)(void))initializeBlock changeBlock:(void(^)(void))changeBlock; {
    _initializeBlock = initializeBlock;
    _changeEnvBlock = changeBlock;
}

- (void)changeEnvironment {
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"选择环境" message:[NSString stringWithFormat:@"当前时%@环境", self.currentModel.environmentName] preferredStyle:UIAlertControllerStyleActionSheet];
    for (DebugModel *debugModel in self.environments) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:debugModel.environmentName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self changeEnvironmentForModel:debugModel];
        }];
        [sheet addAction:action];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [sheet addAction:cancel];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:sheet animated:YES completion:nil];
}

- (void)changeEnvironmentForModel:(DebugModel *)debugModel {
    if ([self.currentModel isEqual:debugModel]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"当前已经是%@环境了", debugModel.environmentName] message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    }else {
        self.currentModel = debugModel;
        if (self.changeEnvBlock) {
            self.changeEnvBlock();
        }
        [[NSUserDefaults standardUserDefaults] setObject:@([self.environments indexOfObject:debugModel]) forKey:ENVIRONMENTKEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (_initializeBlock) {
                _initializeBlock();
            }
        });
    }
}


- (NSString *)webHost {
    return self.currentModel.webHost;
}

- (NSString *)H5Host {
    return self.currentModel.H5Host;
}

@end
